﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Konyvkolcsonzo
{
    public partial class FmAdatok : FormKK
    {
        private string type; // irok, kiadok, mufajok
        List<iro> nevsorI;
        List<mufaj> nevsorM;
        List<kiado> nevsorK;
        Adatok k;
        AdatokEntity tipus;

        public FmAdatok(string type)
        {
            InitializeComponent();
            this.type = type;
            this.pnlLeft.Controls.Add(txbAdatok);
            this.pnlLeft.Controls.Add(lvwAdatok);
            k = new Adatok();
            tipus = new AdatokEntity();
            if (type == tipus.irok)
            {
                nevsorI = new List<iro>();
            }

            else if (type == tipus.kiadok)
            {
                nevsorK = new List<kiado>();
            }

            else if (type == tipus.mufajok)
            {
                nevsorM = new List<mufaj>();
            }

            this.pnlLeft.Controls.Add(txbAdatok);
            this.pnlLeft.Controls.Add(lvwAdatok);
            this.pnlLeft.Controls.Add(btnAdd);
            this.pnlLeft.Controls.Add(btnEdit);
            this.pnlLeft.Controls.Add(btnDelete);
            this.pnlLeft.Controls.Add(btnClose);
            this.pnlLeft.Controls.Add(btnExit);
            this.pnlRight.Controls.Add(btnOk);
            this.pnlRight.Controls.Add(btnCancel);
            this.pnlRight.Controls.Add(txbNev);
            //??Kiado
        }

        private void PopulateI()
        {
            lvwAdatok.Items.Clear();
            nevsorI = k.getIro(txbAdatok.Text);
            for (int i = 0; i < nevsorI.Count; i++)
            {
                ListViewItem sor = new ListViewItem(Convert.ToString(nevsorI[i].id));
                sor.SubItems.Add(Convert.ToString(nevsorI[i].nev));
                lvwAdatok.Items.Add(sor);
            }
            lvwAdatok.Refresh();
        }

        private void PopulateM()
        {
            lvwAdatok.Items.Clear();
            nevsorM = k.getMufaj(txbAdatok.Text);
            for (int i = 0; i < nevsorM.Count; i++)
            {
                ListViewItem sor = new ListViewItem(Convert.ToString(nevsorM[i].id));
                sor.SubItems.Add(Convert.ToString(nevsorM[i].nev));
                lvwAdatok.Items.Add(sor);
            }
        }

        private void PopulateK()
        {
            lvwAdatok.Items.Clear();
            nevsorK = k.getKiado(txbAdatok.Text);
            for (int i = 0; i < nevsorK.Count; i++)
            {
                ListViewItem sor = new ListViewItem(Convert.ToString(nevsorK[i].id));
                sor.SubItems.Add(Convert.ToString(nevsorK[i].nev));
                sor.SubItems.Add(Convert.ToString(nevsorK[i].cim));
                sor.SubItems.Add(Convert.ToString(nevsorK[i].tel));
                lvwAdatok.Items.Add(sor);
            }
        }

        private void PopulatePanelRight(long id)
        {
            txbNev.Text = lvwAdatok.SelectedItems[0].SubItems[1].Text;

            if (type == tipus.kiadok)
            {
                txbCim.Text = lvwAdatok.SelectedItems[0].SubItems[2].Text;
                txbTel.Text = lvwAdatok.SelectedItems[0].SubItems[3].Text;
            }
        }

        private void PopulatePanelRight()
        {
            txbNev.Text = "";

            if (type == tipus.kiadok)
            {
                txbCim.Text = "";
                txbTel.Text = "";
            }
        }

        private void Populate()
        {
            if (type == tipus.irok)
            {
                PopulateI();
            }
            else if (type == tipus.mufajok)
            {
                PopulateM();
            }
            else if (type == tipus.kiadok)
            {
                PopulateK();
            }
        }

        private void FmAdatok_Load(object sender, EventArgs e)
        {
            lvwAdatok.Columns.Add("ID", 0);
            lvwAdatok.Columns.Add("Név", 150);
            txbCim.Visible = false;
            txbTel.Visible = false;
            label3.Visible = false;
            label4.Visible = false;
            label5.Visible = false;
            if (type == tipus.kiadok)
            {
                txbCim.Visible = true;
                txbTel.Visible = true;
                label3.Visible = true;
                label4.Visible = true;
                label5.Visible = true;
                lvwAdatok.Columns.Add("Cím", 150);
                lvwAdatok.Columns.Add("Telefonszám", 100);
            }
            Populate();
        }

        private void txbAdatok_TextChanged(object sender, EventArgs e)
        {
            Populate();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.cmdAdd();
            PopulatePanelRight();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (this.cmdEdit(lvwAdatok.SelectedItems.Count))
            {
                PopulatePanelRight(Convert.ToInt64(lvwAdatok.SelectedItems[0].SubItems[0].Text));
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.cmdDelete(lvwAdatok.SelectedItems.Count))
            {
                if (type == tipus.irok)
                {
                    k.deleteIro(Convert.ToInt64(lvwAdatok.SelectedItems[0].SubItems[0].Text));
                    PopulateI();
                }
                else if (type == tipus.mufajok)
                {
                    k.deleteTipus(Convert.ToInt64(lvwAdatok.SelectedItems[0].SubItems[0].Text));
                    PopulateM();
                }
                else if (type == tipus.kiadok)
                {
                    k.deleteKiado(Convert.ToInt64(lvwAdatok.SelectedItems[0].SubItems[0].Text));
                    PopulateK();
                }
            }

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.cmdClose();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.cmdExit();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.cmdOK();
            if (this.editType())
            {
                if (type == tipus.irok)
                {
                    iro f = new iro();
                    f.nev = txbNev.Text;
                    f.id = Convert.ToInt32( lvwAdatok.SelectedItems[0].SubItems[0].Text);
                    k.updateIro(f);
                    PopulateI();
                }
                else if (type == tipus.mufajok)
                {
                    mufaj f = new mufaj();
                    f.nev = txbNev.Text;
                    f.id = Convert.ToInt32(lvwAdatok.SelectedItems[0].SubItems[0].Text);
                    k.updateTipus(f);
                    PopulateM();
                }
                else if (type == tipus.kiadok)
                {
                    kiado f = new kiado();
                    f.nev = txbNev.Text;
                    f.id = Convert.ToInt32(lvwAdatok.SelectedItems[0].SubItems[0].Text);
                    f.cim = txbCim.Text;
                    f.tel = txbTel.Text;
                    k.updateKiado(f);
                    PopulateK();
                }
            }
            else
            {
                if (type == tipus.irok)
                {
                    iro f = new iro();
                    f.nev = txbNev.Text;
                    k.insertIrok(f);
                    PopulateI();
                }
                else if (type == tipus.mufajok)
                {
                    mufaj f = new mufaj();
                    f.nev = txbNev.Text;
                    k.insertTipus(f);
                    PopulateM();
                }
                else if (type == tipus.kiadok)
                {
                    kiado f = new kiado();
                    f.nev = txbNev.Text;
                    f.cim = txbCim.Text;
                    f.tel = txbTel.Text;
                    k.insertKiado(f);
                    PopulateK();
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.cmdCancel();
        }


    }
}
